package com.raspberryCar.service;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.util.CommandArgumentParser;
import com.raspberryCar.model.Car;

public class AutoCar implements Runnable {

	static GpioPinDigitalInput rightInput;
	static GpioPinDigitalInput leftInput;
	static GpioPinDigitalInput fawInput;
	static GpioPinDigitalInput backInput;
	static Car car;

	public static void main(String[] args) throws InterruptedException {

		car = Car.getInstance();
		GpioController gpio = car.getGpioController();
		PinPullResistance pull = CommandArgumentParser
				.getPinPullResistance(PinPullResistance.PULL_UP // default pin
				); // argument array to search in

		// provision gpio pin as an input pin
		rightInput = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, pull);
		leftInput = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, pull);
		fawInput = gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, pull);
		backInput = gpio.provisionDigitalInputPin(RaspiPin.GPIO_12, pull);

		car.faw();
//		new Thread(new AutoCar());
		// gpio.shutdown();
		while(true){

			if (rightInput.getState().isLow()) {
				System.out.println("1");
			}
			if (leftInput.getState().isLow()) {
				System.out.println("2");
			}
			if (fawInput.getState().isLow()) {
				System.out.println("3");
				car.stop();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (car.getPosition() == 3
						&& car.getToward() == 0) {
					car.ret();
					car.ret();
				}
			}
			if (backInput.getState().isLow()) {
				System.out.println("4");
				car.stop();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (car.getPosition() == 3
						&& car.getToward() == 1) {
					car.faw();
					car.faw();
				}
			}

		
		}
	}

	@Override
	public void run() {
		
		
		while (true) {
			if (rightInput.getState().isLow()) {
				System.out.println("1");
			}
			if (leftInput.getState().isLow()) {
				System.out.println("2");
			}
			if (fawInput.getState().isLow()) {
				System.out.println("3");
				car.stop();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (car.getPosition() == 0
						&& car.getToward() == 0) {
					car.ret();
				}
			}
			if (backInput.getState().isLow()) {
				System.out.println("4");
				car.stop();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (car.getPosition() == 0
						&& car.getToward() == 1) {
					car.faw();
				}
			}

		}

	}

}
